
package com.zking.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.alibaba.fastjson.JSONObject;

public class ServletUtils {

	public static <T> T parseParameter(Class<T> clas,HttpServletRequest request) {
		Map<String ,String[]> params =request.getParameterMap();
		Field [] fields =clas.getDeclaredFields();
		T t=null;
		try {
			t=clas.newInstance();
			StringBuilder sbdValue=new StringBuilder();
			for(int i=0;i<fields.length;i++) {
				sbdValue.setLength(0);
				String fieldName=fields[i].getName();
				String value[] =params.get(fieldName);
				if(value!=null&&value.length>0) {
					for (int s = 0; s < value.length; s++) {
						sbdValue.append(value[s]);
						sbdValue.append(",");
					}
					sbdValue.setLength(sbdValue.length()-1);
				}
				String methodName="set"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
			
					Method method =clas.getDeclaredMethod(methodName, fields[i].getType());
					method.invoke(t,parseType(fields[i].getType().getSimpleName(), 
							sbdValue.length()>0?sbdValue.toString():null));
				
			}
		} 
		catch(Exception e) {
			e.printStackTrace();
		}
		return t;
	}
	/**
	 * 得到实体类中每个参数的类型和对应的字符串的值，然后将字符串转化为对应的值
	 * @param clasName 实体类对应字段的类型
	 * @param value 字段对应的字符串值
	 * @return 对应类型的Object对象
	 */
	public static Object parseType(String clasName, String value) {
		Object obj = null;
		if (value == null) 
			return null;
		switch (clasName) {
		case "int":
			//"" 或者 null
			obj = Integer.parseInt(value);//讲字符串的数字转化为int类型
			break;
		case "Integer":
			obj = Integer.valueOf(value);
			break;
		case "Byte":
			obj = Byte.valueOf(value);
			break;
		case "byte":
			obj = Byte.parseByte(value);
			break;
		case "short":
			obj = Short.parseShort(value);
			break;
		case "Short":
			obj = Short.valueOf(value);
			break;
		case "long":
			obj = Long.parseLong(value);
			break;
		case "Long":
			obj = Long.valueOf(value);
			break;
		case "float":
			obj = Float.parseFloat(value);
			break;
		case "Float":
			obj = Float.valueOf(value);
			break;
		case "double":
			obj = Double.parseDouble(value);
			break;
		case "Double":
			obj = Double.valueOf(value);
			break;
		case "boolean":
			obj = Boolean.parseBoolean(value);
			break;
		case "Boolean":
			obj = Boolean.valueOf(value);
			break;
		default : 
			//以上都不是，就是字符串
			obj = value;
			break;
		}
		return obj;
	}
	public static void send(HttpServletResponse response, JSONObject jsonObject) throws IOException {
		PrintWriter out = response.getWriter();
    	out.print(jsonObject.toString());
		out.flush();
		out.close();
	}
	 public static <T> void exportObject(List<T> objs, Class<T> clas, String path, String fileName) 
				throws Exception {
			//表头
	    	Field[] fields = clas.getDeclaredFields();
	    	String[] head = new String[fields.length];
	    	for (int i = 0; i < fields.length; i++) {
	    		head[i] = fields[i].getName();
	    	}
			
			//创建excel文件
			Workbook workbook = new XSSFWorkbook();
			
			CellStyle cellStyle = workbook.createCellStyle();
			cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
			cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
			
			Sheet sheet = workbook.createSheet("所有菜品数据");
			//创建第一行
			Row row1 = sheet.createRow(0);
			for (int i = 0; i < head.length; i++) {
				row1.createCell(i).setCellValue(head[i]);
				row1.getCell(i).setCellStyle(cellStyle);
			}
			//创建后面的行
			for (int i = 0; i < objs.size(); i++) {
				Row row = sheet.createRow(i+1);
				T t = objs.get(i);
				for (int j = 0; j < fields.length; j++) {
					//使用get方法去取
					String fieldName = fields[j].getName();
					String methodName = "get" + fieldName.substring(0, 1).toUpperCase() 
							+ fieldName.substring(1);
					Method method = clas.getDeclaredMethod(methodName);
					//执行method的get方法
					Object obj = method.invoke(t);
//					System.out.println(obj==null?null:obj.getClass().getSimpleName());
					//设置值的时候，判断
					Cell cell = row.createCell(j);
					setCell(cell, obj);
				}
				//设置样式
				for (int j = 0; j < head.length; j++) {
					row.getCell(j).setCellStyle(cellStyle);
				}
			}
			//设置所有的列都自动适应宽高
			for (int i = 0; i < head.length; i++) {
				sheet.autoSizeColumn(i);
			}
			
			setSizeColumn(sheet, head.length);
			
			//写入文件
			File file = new File(path);
			if (file == null || !file.exists())
				file.mkdirs();
			OutputStream out = new FileOutputStream(path + "\\" + fileName);
			workbook.write(out);
		}
	 
	 
	 public static void setSizeColumn(Sheet sheet, int size) {
	        for (int columnNum = 0; columnNum < size; columnNum++) {
	            int columnWidth = sheet.getColumnWidth(columnNum) / 256;
	            for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
	                Row currentRow;
	                //当前行未被使用过
	                if (sheet.getRow(rowNum) == null) {
	                    currentRow = sheet.createRow(rowNum);
	                } else {
	                    currentRow = sheet.getRow(rowNum);
	                }
	 
	                if (currentRow.getCell(columnNum) != null) {
	                    Cell currentCell = currentRow.getCell(columnNum);
	                    if (currentCell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
	                        int length = currentCell.getStringCellValue().getBytes().length;
	                        if (columnWidth < length) {
	                            columnWidth = length;
	                        }
	                    }
	                }
	            }
	            sheet.setColumnWidth(columnNum, columnWidth * 256);
	        }
	    }
	   public static void setCell(Cell cell, Object obj) {
	    	if (obj == null) {
	    		cell.setCellValue("");
	    		return;
			}
	    	switch (obj.getClass().getSimpleName()) {
	    	//八种数据写全
			case "Integer":
				cell.setCellValue(Integer.parseInt(obj.toString()));
				break;
			case "String":
				cell.setCellValue(obj.toString());
				break;
			case "Double":
				cell.setCellValue(Double.parseDouble(obj.toString()));
				break;
			default:
				//其他数据类型
				cell.setCellValue("");
				break;
			}
	    }
}
